<?php
/**
 * Created by PhpStorm.
 * User: Xzareth
 * Date: 20/05/14
 * Time: 9:33 PM
 */
require 'init.php';
$secure->adminOnly();

if (isset($_POST['submit'])) {

    $productName = $_REQUEST['name'];
    $productDescription = $_REQUEST['description'];
    $unitPrice = $_REQUEST['price'];

    $isFeatured = 0;
    if (isset($_REQUEST['isFeatured'])) $isFeatured = 1;

    $insertProductFormStatus = array();
    $modifyFormStatus = array();

    if ($_REQUEST['submit'] == "X") {
        $productID = $_REQUEST['productID'];

        $modifyFormStatus[] = $product->deleteProduct($productID);
    }

    if ($_REQUEST['submit'] == "Insert") {

        $insertProductFormStatus[] = $product->insertProduct($productName, $productDescription, $unitPrice);
        $insertProductFormStatus[] = $product->uploadImage($productName, $_FILES["file"]["name"]);

    } else if ($_REQUEST['submit'] == "Update") {

        $productID = $_REQUEST['productID'];
        $modifyFormStatus[] = $product->updateProduct($productID, $productName, $productDescription, $unitPrice, $isFeatured);
    }
}
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <link href="css/main.css" rel="stylesheet" type="text/css">
    <script src="javascripts/validateForm.js"></script>
    <title>Products</title>
</head>
<body>
<div id="container">
    <div id="leftCogSplash"><img src="images/cogLeft.png"/></div>
    <div id="rightCogSplash"><img src="images/cogRight.png"/></div>
    <?php include "navbar.php"; ?>
    <div id="content">
        <div id="adminDashboard">
            <div id="returnToDashboard">
                <a href="admin.php"><h3>Return to Dashboard</h3></a>
            </div>
            <?php include "productsDashboard.php"; ?>
            <div id="currentProducts">
                <h3>All Current Products</h3>
            </div>
            <div id="productResults">
                <?php
                $rowNumber = 0;

                if (isset($_GET['productsResults'])) {
                    echo "<script type='text/javascript'>
                            document.getElementById('productResults').style.display='block';
                            document.getElementById('modifyFormContainer').style.display='none';
                            </script>";
                }

                if (isset($_GET['price'])) {
                    switch ($_GET['price']) {
                        case "cheap":
                            $sql = "SELECT * FROM Products WHERE UnitPrice < '20.00'";
                            break;
                        case "midrange":
                            $sql = "SELECT * FROM Products WHERE UnitPrice >= '20.00' AND UnitPrice <= '30.00'";
                            break;
                        case "expensive":
                            $sql = "SELECT * FROM Products WHERE UnitPrice > '30.00'";
                            break;
                    }
                } else if (isset($_GET['productID'])) {
                    $sql = "SELECT * FROM Products WHERE ProductID = '$_GET[productID]'";
                } else {
                    $sql = "SELECT * FROM Products";
                }

                foreach ($db->query($sql) as $row) {
                    $rowNumber++;
                    ?>
                    <table>
                        <thead <?php if ($rowNumber !== 1) echo "style='display: none'" ?>>
                        <tr>
                            <th class="productIDCol">ID</th>
                            <th class="productNameCol">Product Name</th>
                            <th class="descriptionCol">Description</th>
                            <th class="unitPriceCol right">Price</th>
                            <th class="isFeaturedCol center">Featured?</th>
                        </tr>
                        </thead>
                        <tbody>
                        <tr>
                            <td class="productIDCol"><?php echo $row['ProductID']; ?></td>
                            <td class="productNameCol"><?php echo $row['ProductName']; ?></td>
                            <td class="descriptionCol"><?php echo $row['ProductDescription']; ?></td>
                            <td class="unitPriceCol right "><?php echo '$' . $row['UnitPrice']; ?></td>
                            <td class="isFeaturedCol center"><?php echo ($row['IsFeatured'] == 1) ? 'Yes' : 'No'; ?></td>
                        </tr>
                        </tbody>
                    </table>
                <?php
                }
                ?>
            </div>
            <div id="modifyFormContainer">
                <?php
                if (isset($_GET['modifyProducts'])) {
                    echo "<script type='text/javascript'>
                            document.getElementById('productResults').style.display='none';
                            document.getElementById('modifyFormContainer').style.display='block';
                            </script>";
                }
                $rowNumber = 0;
                $sql = "SELECT ProductID, ProductName, ProductDescription, UnitPrice, IsFeatured FROM Products WHERE EOLDate IS NULL";
                foreach ($db->query($sql) as $row) {
                    $rowNumber++;

                    ?>
                    <form id="modifyForm" name="modifyForm" method="post" action="">
                        <table>
                            <thead <?php if ($rowNumber !== 1) echo "style='display: none'" ?>>
                            <tr>
                                <th class="productIDCol"><label>ID</label></th>
                                <th class="productNameCol"><label for='name'>Name</label></th>
                                <th class="descriptionCol"><label for='description'>Description</label></th>
                                <th class="unitPriceCol right"><label for='price'>Price</label></th>
                                <th class="isFeaturedCol center"><label for='isFeatured'>Featured</label></th>
                                <th></th>
                                <th></th>
                            </tr>
                            </thead>
                            <tbody>
                            <tr>
                                <td class="productIDCol"><?php echo $row['ProductID']; ?></td>
                                <td class="productNameCol"><input type='text' name='name' id='name'
                                                                  value='<?php echo $row['ProductName']; ?>'/></td>
                                <td class="descriptionCol"><textarea cols="20" rows="1" name="description"
                                                                     id="description"
                                                                     maxlength="255"><?php echo $row['ProductDescription']; ?></textarea>
                                </td>
                                <td class="unitPriceCol right"><input type='text' name='price' id='price' size="7"
                                                                      value='<?php echo $row['UnitPrice']; ?>'/></td>
                                <td class="isFeaturedCol"><input type='checkbox' name='isFeatured' id='isFeatured'
                                                                 value="1" <?php echo($row['IsFeatured'] == 1 ? 'checked="checked"' : "0"); ?>/>
                                </td>
                                <td class="updateBtn"><input type="submit" name="submit" value="Update"/></td>
                                <td class="deleteBtn"><input type='submit' name='submit' value='X' class='red'
                                                             id="deleteButton"></td>
                            </tr>
                            </tbody>
                        </table>
                        <input type='hidden' name='productID' id='productID' value='<?php echo $row['ProductID']; ?>'/>
                    </form>
                <?php
                }
                ?>
            </div>
            <div id="modifyFormStatusBar">
                <?php
                if (!empty($modifyFormStatus)) {
                    echo "<script type='text/javascript'>document.getElementById('modifyFormStatusBar').style.display = 'block';</script>";
                    echo '<p>' . implode('<p></p>', $modifyFormStatus) . '</p>';
                    echo "<script type='text/javascript'>clearMessage('modifyFormStatusBar', 2000)</script>";
                }
                // close the database connection
                $db = null;
                ?>
            </div>
        </div>
        <?php require 'footer.php' ?>
    </div>

</body>
</html>